package com.module.TraceSource.dao;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;

import com.module.TraceSource.common.TraceSourceConstant;
import com.module.TraceSource.vo.TsEntEvent;
import com.module.TraceSource.vo.TsEventItemRecord;
import com.module.TraceSource.vo.TsEventRecord;


public class TsEventItemRecordDao extends GenericHibernateDao<TsEventItemRecord,Long>{
	private static TsEventItemRecordDao dao;
	private TsEventItemRecordDao(){
		
	}
	
	public synchronized static TsEventItemRecordDao getInstance(){
		if(dao == null){
			dao = new TsEventItemRecordDao();
		}
		return dao;
	}
	
	/**
	 * 判断码是否存在 true=存在,false=不存在
	 * @param session
	 * @param code
	 * @return
	 */
	public boolean isExist(Session session,String code)throws SQLException{
		SQLQuery query=session.createSQLQuery(" select count(*) from ts_event_item_record where c_column_val='"+code+"'");
		long n=((java.math.BigInteger)query.uniqueResult()).longValue();
		return n>0?true:false;
	} 
	
	/**
	 * 根据码查询对应的记录
	 * @param session
	 * @param code
	 * @return
	 * @throws SQLException
	 */
	public TsEventItemRecord findTsEventItemRecordByColVal(Session session,TsEventItemRecord bean)throws SQLException{
		TsEventItemRecord vo = new TsEventItemRecord();
		String code = bean.getColumnVal();
		String orgPid=bean.getOrgPid();
		String orgid = bean.getOrgid();
		Query query = null;
		if("0".equals(orgPid) || orgPid == null){
			query=session.createQuery(" from TsEventItemRecord where columnVal='"+code+"' and type='"+TraceSourceConstant.ITEM_RECORD_TYPE_PRODUCT+"'");
		}else {
			query=session.createQuery(" from TsEventItemRecord where columnVal='"+code+"' and type='"
					+TraceSourceConstant.ITEM_RECORD_TYPE_PRODUCT+"'and orgid='"+orgid+"'");
		}
		List list=query.list();
		if(list!=null&&list.size()>0){
			vo=(TsEventItemRecord)list.get(0);
		}
		
		return vo;
	}
	public List<TsEventItemRecord> findTsEventItemRecordByValue(Session session,List<Long> list)throws SQLException{
		StringBuffer hql1 = new StringBuffer();
		hql1.append(" from TsEventItemRecord as obj where 1=1 and obj.status!='D' and eventid in(");
		for(int i=list.size()-1;i>=0;i--){
			if(i==list.size()-1){
				hql1.append(list.get(i));
			}else{
				hql1.append(",");
				hql1.append(list.get(i));
			}
		}
		hql1.append(")");
		hql1.append(" order by id asc ");
		
		Query query = session.createQuery(hql1.toString());
		//查询操作时间
		java.util.List<TsEventItemRecord> al=query.list();
		TsEventRecordDao rdao = TsEventRecordDao.getInstance();
		TsEntEventDao  tsEntEventDao=TsEntEventDao.getInstance();
		for(int i=0;i<al.size();i++){
			TsEventItemRecord tsEventItemRecord=(TsEventItemRecord)al.get(i);
			Long eventid=tsEventItemRecord.getEventid();
			
			if(eventid!=null){
				TsEventRecord temp=rdao.get(session, eventid);
				tsEventItemRecord.setCreateTime(temp.getCreatetime());

				try {
					PropertyUtils.copyProperties(tsEventItemRecord.getTsEventRecord(),temp);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Long sourceId=temp.getSourceid();
				//根据sourceid获得
				TsEntEvent tsEntEvent=tsEntEventDao.get(session, sourceId);
				tsEventItemRecord.setDesc(tsEntEvent.getDesc());
			}
		}
		return al;
	}
	
	public Page getList(Session session,List<Long> list,Integer start, Integer limit)throws SQLException{
		Page page = new Page();
		try {
			StringBuffer hql1 = new StringBuffer();
			hql1.append(" from TsEventItemRecord as obj where 1=1 and obj.status!='D' and eventid in(");
			StringBuffer hql2 = new StringBuffer();
			hql2.append(" select count(*) from TsEventItemRecord as obj where 1=1 and  obj.status!='D' and eventid in(");
			for(int i=list.size()-1;i>=0;i--){
				if(i==list.size()-1){
					hql1.append(list.get(i));
					hql2.append(list.get(i));
				}else{
					hql1.append(",");
					hql1.append(list.get(i));
					hql2.append(",");
					hql2.append(list.get(i));
				}
			}
			hql1.append(")");
			hql2.append(")");
			hql1.append(" order by id asc ");
			
			Query query = session.createQuery(hql1.toString());
			query.setFirstResult(start).setMaxResults(limit);
			page.setTotal((long) ((Long) session.createQuery(hql2.toString()).uniqueResult()).intValue());
			//查询操作时间
			java.util.List al=query.list();
			TsEventRecordDao rdao = TsEventRecordDao.getInstance();
			for(int i=0;i<al.size();i++){
				TsEventItemRecord tsEventItemRecord=(TsEventItemRecord)al.get(i);
				Long eventid=tsEventItemRecord.getEventid();
				if(eventid!=null){
					tsEventItemRecord.setCreateTime(rdao.get(session, eventid).getCreatetime());
				}
			}
			//end
			page.setRows(al);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
}
